package cc.plumgo.ss.ui.page;

import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.Sprite;

import cc.plumgo.j2me.utils.StringUtils;
import cc.plumgo.ss.core.Constants;

public class OptionPage extends ShowPage {
	public static final int OPT_SOUND = 0;
	public static final int OPT_VOLUME = 1;
	public static final int OPT_STATE_NUM = 2;

	private int sound;
	private int volume;
	private boolean optFlag;
	private Sprite volSprite;
	private Sprite sndSprite;
	private String volName;
	private int volX;
	private int volY;
	private Font font;
	private int delay2 = 5;
	private int color = 0x00000000;
	
	public OptionPage(String imgName, int x, int y) {
		super(imgName, x, y);

		// TODO Read settings
		sound = 6;
		volume = 6;
		optFlag = true;
	}

	public void setSprite(String volName, int x, int y) {
		this.volName = volName;
		volX = x;
		volY = y;
	}

	public void process(int keyStates, Graphics g) {
		super.process(keyStates, g);
		switch (state) {
		case SHOW_HIDE: {
			break;
		}
		case SHOW_SHOW: {
			loadSprites();
			if (((keyStates & GameCanvas.LEFT_PRESSED) != 0)
					|| ((keyStates & GameCanvas.RIGHT_PRESSED) != 0)) {
				optFlag = !optFlag;
			}
			if ((keyStates & GameCanvas.DOWN_PRESSED) != 0) {
				if (optFlag) {
					sound = (++ sound > Constants.OPT_MAX_SOUND) ? Constants.OPT_MAX_SOUND : sound;
				} else {
					volume = (++ volume > Constants.OPT_MAX_VOLUME) ? Constants.OPT_MAX_VOLUME : volume;
				}
			}
			if ((keyStates & GameCanvas.UP_PRESSED) != 0) {
				if (optFlag) {
					sound = (-- sound < 0) ? 0 : sound;
				} else {
					volume = (-- volume < 0) ? 0 : volume;
				}
			}
			
			loadFont();
			g.setFont(font);
			int back = g.getColor();
			g.setColor(0x00000000);
			drawSoundText(g);
			drawVolumeText(g);
			
			if (delay2 == 0) {
				switchColor();
			}
			g.setColor(color);
			
			if (optFlag) {
				drawVolumeText(g);
			} else {
				drawSoundText(g);
			}
			g.setColor(back);
			
			volSprite.setPosition(80, 124);
			volSprite.setFrame(volume);
			volSprite.paint(g);
			
			sndSprite.setPosition(133, 124);
			sndSprite.setFrame(sound);
			sndSprite.paint(g);
			delay2 = (-- delay2 < 0)? 5 : delay2;
			break;
		}
		}
	}
	
	private void switchColor() {
		if (color == 0xFFFFFFFF) {
			color = 0x00000000;
		} else {
			color = 0xFFFFFFFF;
		}
	}
	
	private void loadFont() {
		if (font == null) {
			try {
				font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_LARGE);
			} catch(Exception e) {
				font = Font.getDefaultFont();
			}
		}
	}
	
	private void drawSoundText(Graphics g) {
		StringUtils.adjustDrawString(g, Constants.UI_TXT_OPT_SND, 50, 50, 120, 10);
	}
	
	private void drawVolumeText(Graphics g) {
		StringUtils.adjustDrawString(g, Constants.UI_TXT_OPT_VOL, 50, 179, 120, 10);
	}
	
	private void loadSprites() {
		if (volSprite == null) {
			try {
				Image img = Image.createImage(volName);
				volSprite = new Sprite(img, volX, volY);
				sndSprite = new Sprite(img, volX, volY);

			} catch (Exception e) {
				e.printStackTrace();
			}
			volSprite.defineReferencePixel(volX / 2, volY / 2);
			sndSprite.defineReferencePixel(volX / 2, volY / 2);
			volSprite.setFrame(0);
			sndSprite.setFrame(0);
			sndSprite.setTransform(Sprite.TRANS_MIRROR);
		}
	}
}
